package com.mathslogica.fb;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mathslogica.ML;
import com.mathslogica.MLConstants;

@SuppressWarnings("serial")
public class FBLoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		HttpSession session = req.getSession();
		String redirectUrl = "http://localhost:8888/fbtest";
		String authCode = req.getParameter("code");

		if (authCode == null) {
			Random random = new Random();
			String state = ML.md5(random.nextLong() + "");
			session.setAttribute("state", state);
			String url = "https://www.facebook.com/dialog/oauth?client_id="
					+ MLConstants.FB_APP_ID + "&redirect_uri=" + redirectUrl
					+ "&state=" + state;

			resp.sendRedirect(url);
		}
	}
}
